﻿using IdentityModel;
using IdentityModel.Client;
using IdentityModel.OidcClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using NLog.Fluent;
using NLog.Time;
using Stee.CAP8.Authentication.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Runtime.ConstrainedExecution;
using System.Security;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace DomainLoginTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly Lazy<NLog.ILogger> logger = new Lazy<NLog.ILogger>(() =>
        {
            return NLog.LogManager.GetCurrentClassLogger();
        });

        private LoginContext context;

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                //域账号登录      
                logger.Value.Info("开始");
                var negtiateLogin = new NegotiateLoginClient().UseHttps();
                context = new LoginContext(negtiateLogin);
                var result = await context.LoginAsync(null);
                logger.Value.Info("结束");
                watch.Stop();
                logger.Value.Info(JsonConvert.SerializeObject(result.CurrentLoginMode));
                logger.Value.Info(JsonConvert.SerializeObject(result.User.Identity.Name));
                logger.Value.Info(JsonConvert.SerializeObject(result.User.Identity.IsAuthenticated));
                logger.Value.Info(JsonConvert.SerializeObject(result.User.Identity.AuthenticationType));
               

                SetRichText(this.rchTxtInfo, result);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"ex=>{ex.Message}");
                logger.Value.Info(ex);
            }



        }

        private void SetRichText(RichTextBox rich, UserLoginResult result)
        {
            if (!result.IsError)
            {
                rich.Document.Blocks.Clear();
                Paragraph p = new Paragraph();
                StringBuilder builder = new StringBuilder();
                builder.AppendLine($"access_token={result.AccessToken}");
                foreach (var item in result.User.Claims)
                {
                    builder.AppendLine($"{item.Type}={item.Value}");
                }

                this.txtToken.Text = result.AccessToken;
                p.Inlines.Add(builder.ToString());
                this.rchTxtInfo.Document.Blocks.Add(p);
            }

        }


    }
}
